package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.actiondialog.SaturnActionDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.PageFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnActionNode extends Shape {

	public static final String KEY_NODE_ACTION = "action"; //$NON-NLS-1$
	
	public static final String NAME_NODE_ACTION = Messages.getString("SaturnActionNode.1"); //$NON-NLS-1$

	public static final String KEY_ACTION_NODE_NAME = "_property_name"; //$NON-NLS-1$

	public static final String KEY_ACTION_NODE_KEY = "_property_key"; //$NON-NLS-1$

	public static final String KEY_ACTION_NODE_DESCRITPION = "_property_description"; //$NON-NLS-1$

	public static final String KEY_ACTION_NODE_PARAMETERS = "_property_parameters"; //$NON-NLS-1$

	public static final String SHOW_ACTION_NODE_NAME = "name"; //$NON-NLS-1$

	public static final String SHOW_ACTION_NODE_KEY = "key"; //$NON-NLS-1$

	public static final String SHOW_ACTION_NODE_DESCRITPION = "description"; //$NON-NLS-1$

	public static final String SHOW_ACTION_NODE_PARAMETERS = "parameters"; //$NON-NLS-1$

	private static final long serialVersionUID = 2L;

	private static int number = 1;

	private Image image = Activator.getImage(Images.ACTION_ICON);

	public SaturnActionNode() {
		super.setName(NAME_NODE_ACTION + number++);
	}

	@Override
	public Image getIcon() {
		return image;
	}

	@Override
	public IFigure getFigure() {
		PageFigure figure = new PageFigure();
		if (super.isIschanged() == false) {
			Label imageLabel = new Label(image);
			imageLabel.setLocation(new Point(figure.getLocation().x + 90,
					figure.getLocation().y));
			figure.getImageFigure().add(imageLabel);
			Font nameFont = new Font(null, "Arial", 74, SWT.BOLD); //$NON-NLS-1$
			Label namelabel = new Label(super.getName());
			namelabel.setFont(nameFont);
			figure.getLetterFigure().add(namelabel);
		}

		return figure;
	}

	@Override
	public Node doRefresh() {
		Node root = super.getRoot();

		Node actionNode = SaturnW3CUtil.createChildNode(root, KEY_NODE_ACTION,
				null);

		SaturnW3CUtil.createChildNode(actionNode, SHOW_ACTION_NODE_KEY, super
				.getKey());
		SaturnW3CUtil.createChildNode(actionNode, SHOW_ACTION_NODE_NAME, super
				.getName());
		super.createParemeters(actionNode);

		return actionNode;
	}

	public Dialog getDialog() {
		SaturnActionDialog dialog = new SaturnActionDialog(super.getShell(), this);
		super.reorder(this);
		return dialog;
	}

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnActionNode saturnActionNode = new SaturnActionNode();
		SaturnActionNode saturn = (SaturnActionNode) shape;
		saturnActionNode.setLocation(mousePoint);
		saturnActionNode.setKey(saturn.getKey());
		saturnActionNode.setName(saturn.getName());
		saturnActionNode.setDescription(saturn.getDescription());
		saturnActionNode.setParameters(saturn.getParameters());
		saturnActionNode.setId(saturn.getId());
		saturnActionNode.setNextId(saturn.getNextId());
		return saturnActionNode;
	}

	@Override
	protected void setImage(Image image) {
		this.image = image;
	}

	
public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		String[] suffixes = {"action"}; //$NON-NLS-1$
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace(suffixes , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "action", //$NON-NLS-1$
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();
		String url = "";
		String[] keys = new String[10];
	

	 	infos.put("name", getName());
	 	infos.put("id", getId());
	 	infos.put("expression", getKey());
	 	
	 	//展现构件的表达式链接
	 	if (!"".equals(getKey()) && getKey().endsWith(".action")) {
	 		url = getKey().replace(".action", "");
	 		keys = url.split("/");
	 		if (keys[0].equals(tempnames[0] + "::")) {
	 			if (keys[1].equals(tempnames[1])) {
	 				if (keys[2].equals(tempnames[2])) {
	 					url = "../" + keys[3] + "-action/" + keys[3] + "-action.html";
	 				} else {
	 					url = "../../" + keys[2] + "/" + keys[3] + "-action/" + keys[3] + "-action.html";
	 				}
	 			} else {
	 				url = "../../../" + keys[1] + "/" + keys[2] +"/" 
	 								   + keys[3]  + "-action/" + keys[3] + "-action.html";
	 			}
	 		} else {
	 			url = "../../../../" + keys[0].replace("::", "") + "/" + keys[1] 
	 			                       +"/" + keys[2] +"/" + keys[3] + "-action/" + keys[3] + "-action.html";
	 		}
	 	}
	 	
	 	infos.put("url", url);
	 	infos.put("description", getDescription());
	 	
	 	
		para.setInfourl( name + "-action.html");
		para.setInfoname( name );
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Action.ActionNode")); //$NON-NLS-1$
		para.setImagename("action");
		String templatePath = "template/export/action/action";
		String Path = filePath + File.separator + name + "-action.html";
		FreemarkerUtils.writeTemplateToFile(templatePath, infos, Path);
		
		return para;
	}
}
